package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.ChannelListParam
import com.cheyun.netsalev3.utils.api.*
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback
import com.cheyun.netsalev3.utils.api.dm.DmApiException
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction1

class DmSwitchingDealersRepository: BaseRepository() {
    //获取通道列表
    fun getData(kFunction1: KFunction1<@ParameterName(name = "item") List<ChannelListParam>, Unit>) {
        networkState.postValue(NetworkState.LOADING)
        DmApiClient.getInstance().getChannelmember()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<List<ChannelListParam>,Any>>() {
                    override fun onSuccess(t: DmBaseResp<List<ChannelListParam>,Any>) {
                        networkState.postValue(NetworkState.LOADED)
                        t.list?.apply {
                            kFunction1(this)
                        }
                    }

                    override fun onError(ex: DmApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    //切换经销商
    fun channelhang(channel: Int, moth: () -> Unit) {
        DmApiClient.getInstance().getChannelchange(channel)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<Any,Any>>() {
                    override fun onSuccess(t: DmBaseResp<Any,Any>) {
                        moth()
                    }

                    override fun onError(ex: DmApiException) {

                    }


                })
    }


}